﻿var ChildpageNum = 1;
var Childpagekey = 0;
var NowItemsTemp = "";
var NowItemsNumTemp = 0;
var IsLastPage = false;
var oldpageindex = 0;
if (searchresult != null) {
	NowItemsTemp = searchresult.item;
	NowItemsNumTemp = searchresult.Available;
}
$(document).ready(function() {
	defaultcookiesinput({
		store: search_yahoo_store,
		buynow: search_yahoo_buynow,
		availability: search_yahoo_availability,
		imageFlag: search_rakuten_imageFlag

	});
	defaultcookiesselect({
		item_status: search_yahoo_new,
		Gift_icon: search_yahoo_gift_icon,
		search_area: search_yahoo_loc_cd

	});
	default_Shop_hide();
	//if(NowItemsTemp.length==0&&NowItemsNumTemp==""){
	if (undefined==NowItemsTemp||NowItemsTemp.length == 0) {
		//$("#noresultwillhide").hide();
		$("#noresultwillhide #search_all_items_num").hide();
		$("#noresultwillhide .view-sort-cont").hide();
		$("#noresultwillhide .pagelist").hide();
	}
	loadlang();
	//if(search_lang=="ja"){$(".search_Survey").remove();}
	if (search_lang == "ja") {
		$(".search_Translation").remove();
	}
	$(".search_submit").css({
		"background": "url(/template/images/" + search_lang + "/item_search_1.gif) no-repeat"
	});
	$(".search_submit").hover(function() {
		$(this).css({
			"background": "url(/template/images/" + search_lang + "/item_search_2.gif) no-repeat"
		});
	},
	function() {
		$(this).css({
			"background": "url(/template/images/" + search_lang + "/item_search_1.gif) no-repeat"
		});
	});
	topdesign();
	//search_site
	$("#search_input_site").val(search_site);
	siteclickhistory();
	$("#Minprice").val(search_minprice);
	$("#Maxprice").val(search_maxprice);
	Sethotkey(search_hotkeyword);
	SetCategory(search_category);
	if (searchresult != null) {
		SetJsonData(null, search_show);
	}
	//resetcategory();
	GetSites();
	$('form').bind('submit',
	function() {
		return checkkeyword();
	});
	$(".search_checkbox_sites input").change(function() {
		GetSites();
	});
	$.cookie("sortby", search_sort, options);

	GetHistory();
});
function SetJsonData(JsonData, BOrS) {
	resetSortLink();
	$("#search_result").removeClass();
	$("#ShowType_B_S").val(BOrS);
	$("#search_result").addClass("result_" + BOrS);

	if (BOrS == "b") {
		openloading();
	}
	$("#search_result").empty();
	var resulthtml = "";
	var sortbids = 0,
	sortprice = 0;
	if (BOrS == "b") {
		$("#irretu").addClass("nolink").attr("href", "javascript:void(0);");
		//$("#tateyoko").removeClass("nolink").attr("href","javascript:openurl('Show','s');");
		$("#tateyoko").removeClass("nolink").attr("href", "javascript:SetJsonData(null,'s');");
		$("#irretu img").addClass("viewlist_active").removeClass("viewlist");
		$("#tateyoko img").addClass("viewgallery").removeClass("viewgallery_active");
		resulthtml = "<div class=\"items\" id=\"items_##itemkey#\" data-sortprice=\"##sortprice#\" data-sortbids=\"##sortbids#\" data-sortend=\"##sortend#\" data-key=\"##site#|##itemid#\" onclick=\"SetHistory('##itemkey#','yes');\"><div class=\"item_sites item_site_##site#\"></div><div class=\"item_title\">##title#</div><div class=\"item_img\"><img src=\"##img#\" alt=\"##title#\" class=\"item_main_images\"/></div><div class=\"item_info\"><div style=\"display:none\" id=\"itemid\">##itemid#</div><div style=\"display:none\" id=\"itemsite\">##site#</div><div class=\"content\"><ul id=\"content_info\"><li><span></span><span></span></li><li><span></span><span>##info#</span></li><li><span></span><span></span></li><li><span></span><span></span></li><li><span></span><span></span></li><li><span></span><span></span></li><li><span></span><span></span></li><li><span></span><span></span></li></ul></div><div class=\"item_caption\"><div class=\"item_price\">##price#</div><div class=\"itembutton\"><p>##itembutton#</p><p>##itemgoto#</p><p>##itemwatch#</p></div></div><div class=\"item_store\">##stone#</div></div></div>";
	} else {
		//$("#irretu").removeClass("nolink").attr("href","javascript:openurl('Show','b');");
		$("#irretu").removeClass("nolink").attr("href", "javascript:SetJsonData(null,'b');");
		$("#tateyoko").addClass("nolink").attr("href", "javascript:void(0);");
		$("#irretu img").addClass("viewlist").removeClass("viewlist_active");
		$("#tateyoko img").addClass("viewgallery_active").removeClass("viewgallery");
		resulthtml = "<div class=\"items\" id=\"items_##itemkey#\" data-sortprice=\"##sortprice#\" data-sortbids=\"##sortbids#\" data-sortend=\"##sortend#\" data-key=\"##site#|##itemid#\" onclick=\"ShowItem('##itemkey#','##site#','##itemid#','" + search_type + "','yes')\"><a><div class=\"item_sites item_site_##site#\"></div><div class=\"item_img\"><img src=\"##img#\" alt=\"##title#\" class=\"item_main_images\"/></div><div class=\"item_title\">##title#</div></a><div class=\"item_info\">##item_caption#<div class=\"item_price\">##price#</div><div class=\"item_store\">##stone#</div></div></div>";
	}
	if (search_type == "shopping") {
		resulthtml = resulthtml.replace(/##item_caption#/g, "");
	} else {
		resulthtml = resulthtml.replace(/##item_caption#/g, "<div class=\"item_caption\">##info#</div>");
	}
	//if(JsonData ==null){JsonData = searchresult;}
	if (JsonData != null) {
		NowItemsNumTemp = JsonData.Available > NowItemsNumTemp ? JsonData.Available: NowItemsNumTemp;
		NowItemsTemp = NowItemsTemp.concat(JsonData.item);
	}
	var item_num_str = $("#search_all_items_num").html();
	item_num_str = item_num_str.replace(/##num#/g, "<span>" + NowItemsNumTemp + "</span>").replace(/,/g, "");
	$("#search_all_items_num").html(item_num_str);
	//var oitems = JsonData.item;
	//var items =oitems.concat(oitems);
	//var items = JsonData.item;
	var items = NowItemsTemp;
	var search_resulthtml = "";
	if ($.toNumber(NowItemsNumTemp) > 0) {
		for (var key in items) {
			stonename = items[key].store;
			if (stonename == "1") {
				stonename = "";
			//} else if (stonename.indexOf("gif") > -1) {
			} else if (/.*\.gif/i.test(stonename)) {
				stonename = "<img src='" + stonename + "'/>";
			}
			itemdate = GetTime(items[key].endtime);
			//itemdate = items[key].endtime;
			var itemprice = $.toNumber(items[key].price);

			if (items[key].site == "amazon" && itemprice == "0") {
				itemprice = search_interface["_item_checkprice"];
				if (BOrS != "b") {
					itemprice = "<a href=\"javascript:void(0)\" class=\"redunderline\">" + itemprice + "</a>";
				}
				//search_interface["_item_checkprice"]
				sortprice = 0;
			} else {
				sortprice = itemprice;
				itemprice += search_interface["_yen"];
			}
			sortbids = $.toNumber(items[key].bids);
			addhtml = resulthtml.replace(/##site#/g, items[key].site);
			addhtml = addhtml.replace(/##sortbids#/g, $.toSortNumber(sortbids, key));
			addhtml = addhtml.replace(/##sortprice#/g, $.toSortNumber(sortprice, key));
			addhtml = addhtml.replace(/##sortend#/g, $.toSortTime(items[key].endtime));
			addhtml = addhtml.replace(/##itemid#/g, items[key].itemid);
			addhtml = addhtml.replace(/##title#/g, items[key].title);
			if (items[key].img == "") {
				items[key].img = "/template/images/no_image.gif"
			}
			addhtml = addhtml.replace(/##img#/g, items[key].img);
			addhtml = addhtml.replace(/##price#/g, itemprice);
			addhtml = addhtml.replace(/##info#/g, itemdate);
			addhtml = addhtml.replace(/##stone#/g, stonename);
			addhtml = addhtml.replace(/##itemkey#/g, key);
			if (BOrS == "b") {
				//調用ajax
				var item_button_goto = "<a href=\"#\" id=\"goto\" target=\"_blank\" ><img src=\"/template/images/" + search_lang + "/goDataSite.gif\" /></a>";
				//
				if (search_type == "shopping") {
					buy_img = "ShopAddList.gif";
				} else {
					buy_img = "bigicon.gif";
				}
				var item_button = "";
				var item_towatch = "";
				if (items[key].site == "yahoo" && search_type == "auction") {
					item_button = "<a href=\"" + $.format(url_yahoo_auction, search_lang, items[key].itemid) + "\" target=\"_blank\"><img src=\"/template/images/" + search_lang + "/" + buy_img + "\" /></a>";
					item_button += "<a href=\"" + $.format(url_yahoo_sniper, search_lang, items[key].itemid) + "\" target=\"_blank\"><img src=\"/template/images/" + search_lang + "/Sniperbid.gif\" /></a>";
					item_towatch = "<a href=\"" + $.format(url_watchlist, search_lang, items[key].itemid) + "\" target=\"_blank\"><img src=\"/template/images/" + search_lang + "/BidAddList.gif\" /></a>";
				} else {
					if (items[key].site == "yahoo") {
						siteflag = 1;
					} else if (items[key].site == "rakuten") {
						siteflag = 2;
					} else if (items[key].site == "amazon") {
						siteflag = 3;
					} else if (items[key].site == "bidders") {
						siteflag = 4;
					} else if (items[key].site == "kakaku") {
						siteflag = 5;
					} else if (items[key].site == "calamel") {
						siteflag = 7;
					}else{
						siteflag = 6;
					}
					item_button = "<a href=\"" + $.format(url_shopping, search_lang, items[key].itemid, siteflag) + "\" target=\"_blank\"><img src=\"/template/images/" + search_lang + "/" + buy_img + "\" /></a>";
				}
				item_button += "<a href=\"" + $.format(url_calculator, search_lang,$.toNumber(itemprice)) + "\" target=\"_blank\"><img src=\"/template/images/" + search_lang + "/countPay.gif\" /></a>";
				addhtml = addhtml.replace(/##itembutton#/g, item_button);
				addhtml = addhtml.replace(/##itemgoto#/g, item_button_goto);
				addhtml = addhtml.replace(/##itemwatch#/g, item_towatch);
			}
			search_resulthtml += addhtml;
			//$("#search_result").append(addhtml);
		}
		$("#search_result").html(search_resulthtml);
	}
	Childpagekey = $("#search_result .items").size();
	if (Childpagekey > 0) {
		checkimgload("img.item_main_images", 100, 100);
		KanSea_pageindex("mypagelist", Childpagekey, 24, ChildpageNum);
		if (search_type == "shopping") {
			//$("<img class=\"bg logos stone\" src=\"/template/images/clear.gif\" alt=\"\"/>").prependTo(".item_store");
			$("#search_result .item_store").each(function() {
				if ($(this).html() == "") {
					$("<img src=\"/template/images/premium.gif\" alt=\"\" style=\"margin-top:4px;\"/>").prependTo(this);
				} else {
					$("<img class=\"bg logos stone\" src=\"/template/images/clear.gif\" alt=\"\"/>").prependTo(this);
				}
			});
			$("#search_result .item_store").css({
				"text-align": "center"
			});
		}
		$(".items").click(function() {
			$(this).addClass("itemvisited");
		});
		if (search_lang == "en" && BOrS == "b") {
			$(".item_caption").addClass("item_caption_enfix");
			$("#content_info").css({
				"height": "130px"
			});;
		} else {
			$(".item_caption").removeClass("item_caption_enfix");
		}
		if (BOrS == "b") {
			show_item_content();
			//setTimeout("show_item_content()",($.browser.SafariMobile)?1000:0);
		}
	} else {
		$("#search_result").html("<center style=\"margin-top:50px\">" + search_interface["_Str_Noitems"] + "</center>");
		if (BOrS == "b") {
			$("#floatbox_box").remove();
			$("#fjmybox").remove();
		}
	}
}
//ajax Get Item info
function show_item_content() {
	$(".item_store").css({
		"text-align": "right"
	});
	var url = "./WebService/search.php?dom=JSON&type=" + search_type + "&op=item&";
	$(".result_b .items:visible ").each(function() {
		var watch_url = $(this).find("#goto");
		if (watch_url.attr("href") == "#") {
			//var item_price = msg["price"];
			var itemBox_id = $(this).attr("id");
			var iteminfo = NowItemsTemp[itemBox_id.replace(/items_/g, "")].itemCaption;
			var item_priceBox = $(this).find(".item_price");
			var item_price = item_priceBox.html();
			var content_info = $(this).find("#content_info");
			var itemid = $(this).find("#itemid").html();
			var itemsite = $(this).find("#itemsite").html();
			var Title_url = $(this).find(".item_title");
			watch_url.hide();
			var ajaxurl = url + "site=" + itemsite + "&itemid=" + itemid + "&" + Math.random() * 10000000000;
			//window.open(ajaxurl);
			$.ajax({
				url: ajaxurl,
				dataType: "json",
				beforeSend: function() {
					content_info.html('<img src="./template/images/loading.gif" style="width:40px;height:40px">');
				},
				success: function(msg) {
					if (itemsite == "amazon" && (item_price == 0 || item_price == search_interface["_item_checkprice"])) {
						item_price = "<a href=\"javascript:GetAmazonPrice('" + itemBox_id + "');\">" + search_interface["_item_checkprice"] + "</a>";
						item_priceBox.html(item_price);
					} else {
						//item_price += search_interface["_yen"];
					}
					var content_html, content_html_add = "";
					/*
              if(search_type == "auction"){
                content_html_title = search_interface["_auction"];
                content_html_lefttime = "●<strong>"+ search_interface["_item_lefttime"]+":</strong>"+ GetTime(msg["endtime"]);
                content_html_price = search_interface["_item_price"];
              }else{
                content_html_title = search_interface["_shopping_title"];
                content_html_lefttime = "&nbsp;";
                content_html_price = search_interface["_item_shopprice"];
              }
              var content_html = "<li>●<strong>"+content_html_title+"ID:</strong>"+msg["itemid"]+"</li>";
              content_html += "<li>"+content_html_lefttime+"</li>";
              content_html += "<li>●<strong>"+content_html_price+":</strong><span id=\"itemprice\">"+item_price+"</span></li>";
              */
					if (search_type == "auction") {
						content_html_title = search_interface["_auction"];
						content_html_lefttime = "●<strong>" + search_interface["_item_lefttime"] + "&nbsp;:&nbsp;</strong>" + GetTime(msg["endtime"]);
						content_html_price = search_interface["_item_price"];
						content_html = "<ul><li>●<strong>" + content_html_title + "&nbsp;ID&nbsp;:&nbsp;</strong>" + msg["itemid"] + "</li>";
						content_html += "<li>" + content_html_lefttime + "</li>";
						content_html += "<li>●<strong>" + content_html_price + "&nbsp;:&nbsp;</strong><span id=\"itemprice\">" + item_price + "</span></li>";
						var buynowprice = msg["bidorbuy"];
						var item_bids = msg["bids"];
						if (buynowprice == "" || buynowprice == null) {
							buynowprice = search_interface["_Str_Noprice"];
						} else {
							buynowprice += search_interface["_yen"]
						}
						if (item_bids == "" || item_bids == null) {
							item_bids = "0";
						}
						var item_condition = msg["condition"];
						if (item_condition == "" || item_condition == null) {
							item_condition = search_interface["_Str_Unknow"];
						} else if (item_condition == "new") {
							item_condition = search_interface["_Str_Newitem"];
						} else {
							item_condition = search_interface["_Str_Olditem"];
						}
						content_html += "<li>●<strong>" + search_interface["_item_state"] + "&nbsp;:&nbsp;</strong>" + item_condition + "</li>";
						content_html += "<li>●<strong>" + search_interface["_item_buynow"] + "&nbsp;:&nbsp;</strong>" + buynowprice + "</li>";
						if (msg["store"] == "" || msg["store"] == null) {
							if (itemsite == "yahoo") {
								content_html += "<li>●<strong>" + search_interface["_item_type"] + "&nbsp;:&nbsp;</strong>" + search_interface["_item_seller"] + "</li>";
							} else { //bidders
								content_html += "<li>●<strong>" + search_interface["_item_type"] + "&nbsp;:&nbsp;</strong>" + search_interface["_Str_Unknow"] + "</li>";
							}
						} else {
							content_html += "<li>●<strong>" + search_interface["_item_type"] + "&nbsp;:&nbsp;</strong>" + search_interface["_item_store"] + "</li>";
						}
						content_html += "<li>●<strong>" + search_interface["_item_bids"] + "&nbsp;:&nbsp;</strong>" + item_bids + "</li>";
					} else {
						content_html = "<ul><li>●<strong>" + search_interface["_item_shopprice"] + "&nbsp;:&nbsp;</strong><span id=\"itemprice\">" + item_priceBox.html() + "</span></li>";
						//商品备注
						itemCaption_html = msg["itemCaption"].replace("<.[^>]*>", "");
						itemCaption_html = $.getNotNULL({
							input: itemCaption_html,
							nullvalue: iteminfo,
							delhtml: true
						});

						//itemCaption_html = itemCaption_html.replace(/<.[^>]*>/gi,'');
						content_html_add = "<li>&nbsp;</li>";
						content_html_add += "<li style='width:500px;height:80px'>●<strong>" + search_interface["_item_remark"] + "&nbsp;:&nbsp;</strong><span class=\"itemCaption\">" + itemCaption_html + "</span></li>";
					}
					//送料
					content_html += "<li>●<strong>" + search_interface["_item_post"] + "&nbsp;:&nbsp;</strong>";
					if (msg["postageFlag"] == 0 || msg["postageFlag"] == "送料無料") {
						content_html += search_interface["_item_post_s2"];
					} else {
						content_html += search_interface["_item_post_s1"];
					}
					content_html += "</li>";
					//税
					content_html += "<li>●<strong>" + search_interface["_item_taxFlag"] + "&nbsp;:&nbsp;</strong>";
					if (msg["taxFlag"] == 0) {
						content_html += search_interface["_item_taxFlag_1"];
					} else {
						content_html += search_interface["_item_taxFlag_2"];
					}
					content_html += "</li>";
					if (content_html_add != "") {
						content_html += content_html_add;
					}
					content_html += "</ul>";

					//content_info.html(url);
					content_info.html(content_html);
					$("#" + itemBox_id + " .itemCaption").wordLimit(110);
					watch_url.attr("href", msg["url"]).show();
					Title_url.html("<a href=\"" + msg["url"] + "\" target=\"_blank\">" + Title_url.text() + "</a>");
				}
			});
		}
	});
	$("#floatbox_box").remove();
	$("#fjmybox").remove();
}
function ShowItem(objid, site, itemid, ItemType, isAddhistory) {
	var url = "./WebService/search.php?dom=JSON&site=" + site + "&type=" + ItemType + "&op=item&itemid=" + itemid + "&" + Math.random() * 10000000000;
	//window.open(url);
	$.floatbox({
		ajax: {
			url: url,
			sitename: site,
			itemid: objid,
			itemtype: ItemType,
			//before: "<div id=\"fjmybox\">loading</div>"
			before: "loading",
			regetprice: isAddhistory

		}
	});
	if (isAddhistory == "yes") {
		SetHistory(objid, isAddhistory);
	}
}
google.load("language", "1");